/**
 * Copyright (C) 2014 xspace app Project.
 */

package com.xspace.android.xafcommon.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Base64;
import android.widget.ImageView;

import com.xspace.android.xafcommon.imgload.cache.disc.impl.TotalSizeLimitedDiscCache;
import com.xspace.android.xafcommon.imgload.core.DisplayImageOptions;
import com.xspace.android.xafcommon.imgload.core.ImageLoader;
import com.xspace.android.xafcommon.imgload.core.ImageLoader.LoadType;
import com.xspace.android.xafcommon.imgload.core.ImageLoaderConfiguration;
import com.xspace.android.xafcommon.imgload.core.assist.ImageScaleType;
import com.xspace.android.xafcommon.imgload.utils.StorageUtils;

import java.io.File;

/**
 * @version V1.0.0
 * @ClassName: ImageUtil
 * @author:mawen
 * @Date：2014-3-28
 * @Description: TODO
 */
public class ImageUtils {
    public static ImageLoader imageLoader;
    private static DisplayImageOptions options;


    public static void init(Context mContext) {
        imageLoader = ImageLoader.getInstance();
        File cacheDir = StorageUtils.getOwnCacheDirectory(mContext, "ctnq/img/cache");
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                mContext)
                .memoryCacheExtraOptions(800, 480)
                .threadPoolSize(5)
                .threadPriority(Thread.MIN_PRIORITY + 3)
                .discCache(new TotalSizeLimitedDiscCache(cacheDir,
                        1024 * 1024 * 50)).build();
        imageLoader.init(config);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(BaseRProxy.ic_contact_picture)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();
    }

    public static void showImage(String url, ImageView iv, LoadType loadType)
            throws RuntimeException {
        if (imageLoader == null || options == null) {
            throw new RuntimeException("ImageUtil not init!");
        }
        if (loadType == null) {
            ImageLoader.sLoadType = LoadType.URL;
        } else {
            ImageLoader.sLoadType = loadType;
        }
        imageLoader.displayImage(url, iv, options);
    }

    public static void showImage(String url, ImageView iv, LoadType loadType,
                                 int imageDefault) throws RuntimeException {
        if (imageLoader == null || options == null) {
            throw new RuntimeException("ImageUtil not init!");
        }
        if (loadType == null) {
            ImageLoader.sLoadType = LoadType.URL;
        } else {
            ImageLoader.sLoadType = loadType;
        }
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(imageDefault).cacheOnDisc(true)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();
        imageLoader.displayImage(url, iv, options);
    }

    public static Bitmap stringtoBitmap(String string) {
        // 将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,

                    bitmapArray.length);
        } catch (Exception e) {
//			MobclickAgent.reportError(MyApplication.mContext, e);
            e.printStackTrace();
        }

        return bitmap;
    }

    public static boolean isImageUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return false;
        } else {
            if (url.endsWith(".jpg")
                    || url.endsWith(".png")
                    || url.endsWith(".bmp")
                    || url.endsWith(".jpeg")
                    || url.endsWith(".gif ")) {
                return true;
            } else {
                return false;
            }
        }
    }

    /***
     * 按比例缩放图片
     *
     * @param bitmap
     * @param maxW
     * @param maxH
     * @return
     */
    public static Bitmap resizeImage(Bitmap bitmap, int maxW, int maxH) {
        Bitmap BitmapOrg = bitmap;
        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = maxW;
        int newHeight = maxH;
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        float scale = scaleHeight > scaleWidth ? scaleWidth : scaleHeight;
        LogUtil.v("imageView", "onImageLoadingEnded resizeImage :" + width + " height:" + height + " scale:" + scale);
        if (scale < 1) {
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            // if you want to rotate the Bitmap   旋转
            // matrix.postRotate(45);
            bitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                    height, matrix, true);
        }
        return bitmap;
    }
}
